package org.sixtynine.stock.controller;

import java.util.List;

import javax.validation.Valid;

import org.sixtynine.stock.entity.BaseEntity;
import org.sixtynine.stock.entity.FilterCategory;
import org.sixtynine.stock.service.GenericService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author Thilan
 * 
 */
@Controller
public class FilterCategoryController {

	@Autowired
	private GenericService genericService;

	@RequestMapping(value = "/admin/filtercategory/add")
	public ModelAndView addFilter() {
		ModelAndView modelAndView = new ModelAndView(
				"/admin/filtercategory/add");
		modelAndView.addObject("filterCategory", new FilterCategory());

		return modelAndView;
	}

	@RequestMapping(value = "/admin/filtercategory/add/process")
	public ModelAndView addingFilter(
			@ModelAttribute @Valid FilterCategory filterCategory,
			BindingResult result) {

		if (!result.hasErrors()) {
			genericService.saveOrUpdate(filterCategory);

			ModelAndView modelAndView = new ModelAndView(
					"/admin/filtercategory/list");

			List<BaseEntity> filterCategoryList = genericService
					.findAll(FilterCategory.class);
			modelAndView.addObject("filterCategoryList", filterCategoryList);

			String message = "FilterCategory was successfully added.";
			modelAndView.addObject("message", message);

			return modelAndView;
		} else {
			ModelAndView modelAndView = new ModelAndView(
					"/admin/filtercategory/add");

			return modelAndView;
		}
	}

	@RequestMapping(value = "/admin/filtercategory/list")
	public ModelAndView listOfFilters() {
		ModelAndView modelAndView = new ModelAndView(
				"/admin/filtercategory/list");

		List<BaseEntity> filterCategoryList = genericService
				.findAll(FilterCategory.class);
		modelAndView.addObject("filterCategoryList", filterCategoryList);

		return modelAndView;
	}

	@RequestMapping(value = "/admin/filtercategory/edit/{id}", method = RequestMethod.GET)
	public ModelAndView editFilter(@PathVariable Integer id) {
		ModelAndView modelAndView = new ModelAndView(
				"/admin/filtercategory/edit");
		BaseEntity filterCategory = genericService.findById(id,
				FilterCategory.class);

		modelAndView.addObject("filterCategory", filterCategory);

		return modelAndView;
	}

	@RequestMapping(value = "/admin/filtercategory/edit/{id}", method = RequestMethod.POST)
	public ModelAndView edditingTeam(
			@ModelAttribute @Valid FilterCategory filterCategory,
			BindingResult result, @PathVariable Integer id) {

		if (!result.hasErrors()) {
			genericService.saveOrUpdate(filterCategory);
			ModelAndView modelAndView = new ModelAndView(
					"/admin/filtercategory/list");

			List<BaseEntity> filterCategoryList = genericService
					.findAll(FilterCategory.class);
			modelAndView.addObject("filterCategoryList", filterCategoryList);

			String message = "FilterCategory was successfully edited.";
			modelAndView.addObject("message", message);

			return modelAndView;
		} else {
			ModelAndView modelAndView = new ModelAndView(
					"/admin/filtercategory/edit");

			modelAndView.addObject("filterCategory", filterCategory);

			return modelAndView;
		}
	}

	@RequestMapping(value = "/admin/filtercategory/delete/{id}", method = RequestMethod.GET)
	public ModelAndView deleteFilter(@PathVariable Integer id) {

		genericService.delete(id, FilterCategory.class);
		ModelAndView modelAndView = new ModelAndView(
				"/admin/filtercategory/list");
		List<BaseEntity> filterCategoryList = genericService
				.findAll(FilterCategory.class);
		modelAndView.addObject("filterCategoryList", filterCategoryList);
		String message = "FilterCategory was successfully deleted.";
		modelAndView.addObject("message", message);
		return modelAndView;
	}

}
